package com.feihong.common.util;

import com.feihong.activity.entity.ProductKeyRelDTO;
import com.feihong.activity.exception.CustomException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileUtils {
    protected static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
    public static Map<String, String> getMap(String pathName,Integer taskId) throws IOException {
        Map<String, String> maps = new HashMap<>();

        File file = new File(pathName);
        if (!file.exists() || file.isDirectory()) {
            throw new CustomException("文件未找到");
        }
        if(taskId == 0 || taskId == null){
            throw new CustomException("任务id错误");
        }
        XSSFWorkbook workbook = new XSSFWorkbook(pathName);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        logger.debug("该工作表共有" + lastRowNum + "行数据");
        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow cells = sheet.getRow(i);

            ProductKeyRelDTO rel = new ProductKeyRelDTO();
            cells.getCell(2).setCellType(CellType.STRING);
            rel.setOtherProductKey(cells.getCell(2).getStringCellValue());
            cells.getCell(3).setCellType(CellType.STRING);
            rel.setLocalProductKey(cells.getCell(3).getStringCellValue());
            rel.setPushTaskId(Integer.toString(taskId));
            maps.put(rel.getLocalProductKey(), rel.getOtherProductKey());
        }
        return maps;
    }
}
